﻿using Microsoft.Extensions.DependencyInjection;

namespace Services
{
    public static partial class ServiceExtensions
    {
        public static void ConfigureServices(this IServiceCollection services)
        {
            //ConfigureService(services, "Services");
            //services.AddScoped<IAccountService, AccountService>();
            //services.AddScoped<ITokenService, TokenService>();
            //services.AddScoped<INavigationService, NavigationService>();
        }


        //private static void ConfigureService(IServiceCollection services, string assemblyName)
        //{
        //    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
        //    var transentType = typeof(ITransentDependency);
        //    var scopeType = typeof(IScopeDependency);
        //    var singletonType = typeof(ISingletonDependency);
        //    var dependencyType = typeof(IDependency);
        //    var types = assembly.GetTypes().Where(x =>
        //        x.IsInterface
        //        && dependencyType.IsAssignableFrom(x)
        //        && x != transentType
        //        && x != scopeType
        //        && x != singletonType
        //        && x != dependencyType);

        //    foreach (var item in types)
        //    {
        //        var implementType = assembly.GetTypes()
        //            .Where(x => x.IsClass && item.IsAssignableFrom(x))
        //            .FirstOrDefault();
        //        if (implementType != null)
        //        {
        //            var dependency = item.GetInterfaces().Where(x => x == scopeType || x == singletonType || x == transentType).ToList();
        //            if (dependency.Count > 1)
        //            {
        //                throw new Exception($"{implementType.FullName} 有多个生命周期类型");
        //            }
        //            var injectTypeName = (dependency.Count == 0 ? transentType : dependency.Single()).Name;
        //            if (injectTypeName == singletonType.Name)
        //            {
        //                services.AddSingleton(item, implementType);
        //            }
        //            else if (injectTypeName == scopeType.Name)
        //            {
        //                services.AddScoped(item, implementType);
        //            }
        //            else
        //                services.AddTransient(item, implementType);
        //        }
        //    }
        //}

    }
}
